<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库时间" prop="finishtime">
        <el-input
          v-model="queryParams.finishtime"
          placeholder="请输入入库时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:inbounds:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:inbounds:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:inbounds:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:inbounds:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inboundsList" @selection-change="handleSelectionChange" @row-dblclick="handleViewDetail">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="入库单名" align="center" prop="number"/>
      <el-table-column label="处理人" align="center" prop="handledby" />
      <el-table-column label=" 入库时间" align="center" prop="finishtime" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table
            :data="props.row.inboundItemsList"
            style="width: 95%; margin-left: 50px; margin-bottom: 20px;"
            border>
            <el-table-column label="序号" type="index" width="50" align="center" />
            <el-table-column label="货物名称" prop="itemName" />
            <el-table-column label="货物编号" prop="itemNumber" />
            <el-table-column label="货物规格" prop="itemModel" />
            <el-table-column label="货物单位" prop="itemUnit" />
            <el-table-column label="仓库名称" prop="stockName" />
            <el-table-column label="数量" prop="actualWeight" />
            <el-table-column label="价格" prop="settlementPrice" />
            <el-table-column label="不含税价" prop="excludingPrice" />
            <el-table-column label="含税价" prop="settlementAmount" />
<!--            <el-table-column label="包类型" prop="packageType" />-->
<!--            <el-table-column label="入库时间" prop="finishtime" />-->
            <el-table-column label="备注" prop="remarks" />
          </el-table>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inbounds:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inbounds:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处理人" prop="handledby">
          <el-input v-model="form.handledby" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="复核人" prop="reviewer">
          <el-input v-model="form.reviewer" placeholder="请输入复核人" />
        </el-form-item>
        <el-form-item label="复核时间" prop="reviewTime">
          <el-input v-model="form.reviewTime" placeholder="请输入复核时间" />
        </el-form-item>
        <el-form-item label=" 入库时间" prop="finishtime">
          <el-input v-model="form.finishtime" placeholder="请输入 入库时间" />
        </el-form-item>
        <el-divider content-position="center">入库单项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddInboundItems">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteInboundItems">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="inboundItemsList" :row-class-name="rowInboundItemsIndex" @selection-change="handleInboundItemsSelectionChange" ref="inboundItems">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="货物名称" prop="itemName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemName" placeholder="请输入货物名称" />
            </template>
          </el-table-column>
          <el-table-column label="货物编号" prop="itemNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemNumber" placeholder="请输入货物编号" />
            </template>
          </el-table-column>
          <el-table-column label="货物规格" prop="itemModel" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemModel" placeholder="请输入货物规格" />
            </template>
          </el-table-column>
          <el-table-column label="货物单位" prop="itemUnit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemUnit" placeholder="请输入货物单位" />
            </template>
          </el-table-column>
          <el-table-column label="仓库名称" prop="stockName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.stockName" placeholder="请输入仓库名称" />
            </template>
          </el-table-column>
          <el-table-column label="包类型" prop="packageType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.packageType" placeholder="请选择包类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="入库时间" prop="finishtime" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.finishtime" placeholder="请输入入库时间" />
            </template>
          </el-table-column>
          <el-table-column label=" 备注" prop="remarks" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remarks" placeholder="请输入 备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInbounds, getInbounds, delInbounds, addInbounds, updateInbounds } from "@/api/system/inbounds"

export default {
  name: "Inbounds",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedInboundItems: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库单表格数据
      inboundsList: [],
      // 入库单项表格数据
      inboundItemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        finishtime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    /** 双击查看详情 */
    handleViewDetail(row) {
      this.$router.push({
        name: 'InboundsDetail',
        params: { id: row.id },
        query: { mode: 'view' }
      });
    },

    /** 查询入库单列表 */
    getList() {
      this.loading = true
      listInbounds(this.queryParams).then(response => {
        this.inboundsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        createOrgId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
        isDelete: null,
        orgId: null,
        createOrgName: null,
        sqare01: null,
        sqare02: null,
        sqare03: null,
        sqare04: null,
        sqare05: null,
        sqare06: null,
        number: null,
        planId: null,
        planNumber: null,
        planName: null,
        contractNumber: null,
        contractName: null,
        type: null,
        sendoutCompany: null,
        receiveCompany: null,
        carrierCompany: null,
        licensePlate: null,
        carType: null,
        driverId: null,
        driverName: null,
        drivertelephone: null,
        driverIdCard: null,
        fwid: null,
        handledby: null,
        handledbyOrg: null,
        reviewerId: null,
        reviewer: null,
        reviewbyOrg: null,
        reviewTime: null,
        checkState: null,
        waybillState: null,
        checkUp: null,
        checkUpBy: null,
        checkUpDate: null,
        businessType: null,
        code: null,
        currentUnit: null,
        dispatchtime: null,
        signinState: null,
        signintime: null,
        fAdmissionTime: null,
        finishtime: null,
        contactId: null,
        manufactor: null,
        remarks: null,
        handledbyOrgId: null,
        carnoColor: null,
        carId: null
      }
      this.inboundItemsList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 跳转到详情页，模式为新增
      this.$router.push({
        name: 'InboundsDetail',
        params: { id: 'new' },
        query: { mode: 'add' }
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      // 跳转到详情页，模式为编辑
      const id = row.id || this.ids[0];
      this.$router.push({
        name: 'InboundsDetail',
        params: { id: id },
        query: { mode: 'edit' }
      });
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset()
    //   this.open = true
    //   this.title = "添加入库单"
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset()
    //   const id = row.id || this.ids
    //   getInbounds(id).then(response => {
    //     this.form = response.data
    //     this.inboundItemsList = response.data.inboundItemsList
    //     this.open = true
    //     this.title = "修改入库单"
    //   })
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.inboundItemsList = this.inboundItemsList
          if (this.form.id != null) {
            updateInbounds(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInbounds(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除入库单编号为"' + ids + '"的数据项？').then(function() {
        return delInbounds(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 入库单项序号 */
    rowInboundItemsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 入库单项添加按钮操作 */
    handleAddInboundItems() {
      let obj = {}
      obj.itemName = ""
      obj.itemNumber = ""
      obj.itemModel = ""
      obj.itemUnit = ""
      obj.stockName = ""
      obj.packageType = ""
      obj.packageNum = ""
      obj.cube = ""
      obj.actualWeight = ""
      obj.transportPrice = ""
      obj.excludingPrice = ""
      obj.finishtime = ""
      obj.remarks = ""
      this.inboundItemsList.push(obj)
    },
    /** 入库单项删除按钮操作 */
    handleDeleteInboundItems() {
      if (this.checkedInboundItems.length == 0) {
        this.$modal.msgError("请先选择要删除的入库单项数据")
      } else {
        const inboundItemsList = this.inboundItemsList
        const checkedInboundItems = this.checkedInboundItems
        this.inboundItemsList = inboundItemsList.filter(function(item) {
          return checkedInboundItems.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleInboundItemsSelectionChange(selection) {
      this.checkedInboundItems = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inbounds/export', {
        ...this.queryParams
      }, `inbounds_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
